<?php

					$brand=Brand::getFromName($_POST['brand']);
					$_SESSION['newProduct']['brand_id']=$brand['id'];
					
					
					$_SESSION['newProduct']['famille_id']=$_POST['subsubcategory'];
					
					$subsubcategory=new SubSubcategory($_SESSION['newProduct']['famille_id']);
					
					$subsubcategory->load();
					
					$subcategory=SubSubcategory::getSubcategory($_SESSION['newProduct']['famille_id']);
					$category=SubSubcategory::getCategory($_SESSION['newProduct']['famille_id']);
					$_SESSION['newProduct']['category']=$category;
					$_SESSION['newProduct']['subcategory']=$subcategory;
					$_SESSION['newProduct']['subsubcategory']=$subsubcategory;
					
	
					$_SESSION['newProduct']['nom']=$_POST['name_product'];
				
					
					$_SESSION['newProduct']['descriptif']=$_POST['description'];
					
				
				
					$_SESSION['newProduct']['entretient']=$_POST['entretien'];
					$_SESSION['newProduct']['ref']=$_POST['reference'];
					$_SESSION['newProduct']['composition']=$_POST['composition'];
					
					
					$_SESSION['newProduct']['prix']=$_POST['price'];
				
					
					if (!empty($_POST['quantities'][0])){
						$_SESSION['newProduct']['stock']=0;
					}
					else $_SESSION['newProduct']['stock']=$_POST['stock'];
					
					
					if(!empty($_SESSION['newProduct']['complement'][0][0])){
						foreach ($_SESSION['newProduct']['complement'] as $compl){
							$compl->delete();
						}
						unset($_SESSION['newProduct']['complement']);
						
					}
					
					

					for($i=0;$i<20;$i++){
						if((!empty($_POST['quantities'][$i]))&&((!empty($_POST['colors'][$i]))||(!empty($_POST['sizes'][$i])))) {
							$_SESSION['complement'.$i]=new ProductComplement();
							$_SESSION['complement'.$i]['nom']=$_POST['colors'][$i].' '.$_POST['sizes'][$i];
							$_SESSION['complement'.$i]['stock']=$_POST['quantities'][$i];
							
						}
											
					}				
					
					//$_SESSION['newProduct']['id'] =  Product::existProduct($_SESSION['newProduct']['nom'], $_SESSION['vendor']['id']);
						
					$_SESSION['newProduct']->save();
						
						
					$_SESSION['newProduct']->deleteLabels();
					$labs=array();	
					if (!empty($_POST['labels'][0])) {$_SESSION['label0']=new Label(); $_SESSION['label0']['id']=$_POST['labels'][0]; $labs[]= $_SESSION['label0']; $_SESSION['newProduct']->addLabel($_SESSION['label0']['id']);}
					if (!empty($_POST['labels'][1])) {$_SESSION['label1']=new Label(); $_SESSION['label1']['id']=$_POST['labels'][1]; $labs[]= $_SESSION['label1']; $_SESSION['newProduct']->addLabel($_SESSION['label1']['id']);}	
					if (!empty($_POST['labels'][2])) {$_SESSION['label2']=new Label(); $_SESSION['label2']['id']=$_POST['labels'][2]; $labs[]= $_SESSION['label2'];$_SESSION['newProduct']->addLabel($_SESSION['label2']['id']);} 
					if (!empty($_POST['labels'][3])) {$_SESSION['label3']=new Label(); $_SESSION['label3']['id']=$_POST['labels'][3]; $labs[]= $_SESSION['label3'];$_SESSION['newProduct']->addLabel($_SESSION['label3']['id']);} 
					if (!empty($_POST['labels'][4])) {$_SESSION['label4']=new Label(); $_SESSION['label4']['id']=$_POST['labels'][4]; $labs[]= $_SESSION['label4'];$_SESSION['newProduct']->addLabel($_SESSION['label4']['id']);} 
					$tpl->assign('labs',$labs);
					$_SESSION['newProduct']->load();
				
				
				
							$_SESSION['complements']=array();
						for($i=0;$i<20;$i++){
							if((!empty($_POST['quantities'][$i]))&&((!empty($_POST['colors'][$i]))||(!empty($_POST['sizes'][$i])))) {
								$_SESSION['complement'.$i]['produit_id']=$_SESSION['newProduct']['id'];
								$_SESSION['complement'.$i]->save();
								$_SESSION['complements'][]=$_SESSION['complement'.$i];
								
								
							}
						}
						
			
					
			
					$_SESSION['newProduct']->save();
					
					
					
				
	


?>